﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using XbsWebAPI.Application.Feedback.Dto;
using XbsWebAPI.Application.QueryModel;
using XbsWebAPI.Core.Repositories;
using XbsWebAPI.Core.Repositories.Feedback;
using XbsWebAPI.EntityFrameworkCore.Entities.FeedBack;

namespace XbsWebAPI.Application.Feedback
{
    public class FeedBackAppService : IFeedBackAppService
    {
        private readonly IFeedBackRepository _feedBackRepository;
        private readonly IGuestRepository _guestRepository;
        private readonly IMapper _IMapper;
        public FeedBackAppService(IFeedBackRepository feedBackRepository, IMapper mapper,IGuestRepository guestRepository)
        {
            _feedBackRepository = feedBackRepository;
            _guestRepository = guestRepository;
            _IMapper = mapper;
        }

        /// <summary>
        /// 新增意见反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ResultDto<string>> AddFeedBack(FeedBackDto model)
        {
            var resultdto = new ResultDto<string>();
            var entity = _IMapper.Map<FeedBack>(model);
            var guest = _guestRepository.AsQueryable().FirstOrDefault(a => a.PhoneNumber == model.GuestPhone);
            if(guest == null)
            {
                resultdto.Code = -1;
                resultdto.Message = "找不到房客信息";
            }
            entity.GuestId = guest.Id;
            var reuslt = _feedBackRepository.CreateAsync(entity);
            if (reuslt.Result == null)
            {
                resultdto.Code = -1;
                resultdto.Message = "意见反馈失败";
            }
            else
            {
                resultdto.Code = 0;
                resultdto.Message = "意见反馈成功";
            }
            return Task.FromResult(resultdto);
        }

        public Task<ResultDto<string>> DeleteFeedBack(Guid Id)
        {
            var result = new ResultDto<string>();
            try
            {
                var entity = _feedBackRepository.DeleteAsync(Id);
                result.Message = "删除成功";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                return Task.FromResult(result);
            }
            return Task.FromResult(result);
        }

        /// <summary>
        /// 分页查询/条件查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultDto<PageListDto<ListFeedBackDto>> GetFeedBackPagedList(QueryFeedBack model)
        {
            var result = new ResultDto<PageListDto<ListFeedBackDto>>();
            try
            {
                var guestQuery = _feedBackRepository.AsQueryable();
                //判断查询条件是否为空
                if (!string.IsNullOrEmpty(model.Data))
                {
                    guestQuery = guestQuery.Where(a => a.GuestName.Contains(model.Data) ||
                   a.GuestPhone.Contains(model.Data) || a.Content.Contains(model.Data));
                }

                guestQuery = guestQuery.OrderByDescending(a => a.CreationDate);
                var pagedList = guestQuery.ToPagedList(model.PageNumber ?? 1, model.PageSize ?? 10); 

                PageListDto<ListFeedBackDto> resultPaged = new PageListDto<ListFeedBackDto>()
                {
                    PageNumber = pagedList.PageNumber,
                    PageSize = pagedList.PageSize,
                    HasPreviousPage = pagedList.HasPreviousPage,
                    HasNextPage = pagedList.HasNextPage,
                    TotalCount = pagedList.TotalItemCount
                };

                var dtos = pagedList.Select(a => _IMapper.Map<ListFeedBackDto>(a)).ToList();
                resultPaged.Dtos = dtos;
                result.Message = "查询成功";
                result.Result = resultPaged;
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                return result;
            }
            return result;
        }
    }
}
